package com.stark.friendrequest;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button statusButton;
	private SharedPreferences prefs;
	private String lastUsedString, todayString;
	private Date nextAvailable, today;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		
		if (prefs.getString("first_use", "0") != "0"){
		
		if (prefs.getString("time_remaining","Enter Time") != "") {
			lastUsedString = prefs.getString("time_remaining","30");
		}
		
		todayString = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
		System.out.println(lastUsedString);
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aa");
		SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd MMM yyyy hh:mm:ss");
		
		try {
			today = dateFormat2.parse(todayString);
			System.out.println(today);
			nextAvailable = dateFormat2.parse(lastUsedString);
			int month = nextAvailable.getMonth();
			nextAvailable.setMonth(month+1);
		} catch (ParseException e) {
			System.out.println("CATCH");
			e.printStackTrace();
		}
		
		System.out.println(today + "***" + nextAvailable);

		statusButton=(Button)findViewById(R.id.check_status);
		statusButton.setOnClickListener(new Button.OnClickListener(){
		public void onClick(View arg0) {
			//Provide feedback when the button is pressed.
			//statusButton.setBackgroundResource(R.drawable.buttonpressed);
			
			if (lastUsedString == "30"){
				Intent i = new Intent(MainActivity.this, RequestAvailable.class);
				startActivity(i);
			
			//if today is after next available
		}	else if(today.compareTo(nextAvailable) < 0){
				Intent i = new Intent(MainActivity.this, RequestUnavailable.class);
				startActivity(i);
			}	else {
					Intent i = new Intent(MainActivity.this, RequestAvailable.class);
					startActivity(i);
				}
	    }}); 
		} else {
			Intent i = new Intent(MainActivity.this, EnterDetails.class);
			startActivity(i);
		}
	}

/*	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{	//inflate the android option_menu from XML layout
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.option_menu, menu);
	    return true;
	}*/
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.menu_settings:
	            showSettings(); //if settings button is pressed call the goHome method
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	public void showSettings(){
		//Create a method to handle a button press on the menu
		Intent i = new Intent(this, Settings.class);
		startActivity(i);
	}

}
